home *** CD-ROM | disk | FTP | other *** search
- /* RFILE.C Random file access functions */
-
- #include "rtbl.h"
- #include <FCNTL.H>
- #include "sys\stat.h"
- #include "io.h"
-
- int ropent(filspc,tbl,dat,sz) /* open file for random access w/truncation */
-
- char *filspc; /* filespec string */
- struct RTBL *tbl; /* file access table in rtbl.h */
- char *dat; /* record structure, data transfer area */
- int sz; /* sizeof dat */
-
- { int port,open();
- long ef,lseek();
-
- port=open(filspc,O_RDWR | O_CREAT | O_BINARY | O_TRUNC,S_IREAD | S_IWRITE );
- if(port<0) return(1); /* error return */
- tbl->fprt=port;
- tbl->szdta=sz;
- ef=lseek(port,0L,2);
- ef+=1;
- tbl->hrn=ef/tbl->szdta;
- tbl->dta=dat;
- return(0); /* no error */
- }
-
- int ropen(filspc,tbl,dat,sz) /* open file for random access */
-
- char *filspc; /* filespec string */
- struct RTBL *tbl; /* file access table in rtbl.h */
- char *dat; /* record structure, data transfer area */
- int sz; /* sizeof dat */
-
- { int port,open();
- long ef,lseek();
-
- port=open(filspc,O_RDWR | O_BINARY,S_IREAD | S_IWRITE );
- if(port<0) return(1); /* error return */
- tbl->fprt=port;
- tbl->szdta=sz;
- ef=lseek(port,0L,2);
- ef+=1;
- tbl->hrn=ef/tbl->szdta;
- tbl->dta=dat;
- return(0); /* no error */
- }
-
-
- int rfile(ftbl,rec,mode) /* random file access */
- /* assumes port assigned */
- struct RTBL *ftbl; /* file access table */
- int rec; /* record to read-write */
- char mode; /* r = read, w = write */
-
- {
-
- int stat,read(),write();
- long pos,lseek();
-
- stat=0;
-
- if(rec > ftbl->hrn) {
- if(mode=='w')
- ftbl->hrn++;
- rec=ftbl->hrn;
- }
- rec--;
- pos = (long)rec * (long)ftbl->szdta;
- lseek(ftbl->fprt,pos,0);
- switch(mode) {
- case'r':
- stat=read(ftbl->fprt,ftbl->dta,ftbl->szdta);
- break;
- case'w':
- stat=write(ftbl->fprt,ftbl->dta,ftbl->szdta);
- break;
- }
-
- if(stat <= 0)
- return(stat-1); /* -1=EOF, < (-1) = error */
- else
- return(rec+1);
- }
-
-
-
- int rclose(ctbl) /* close a random file */
-
- struct RTBL *ctbl; /* file access table */
-
- {
- int close();
-
- close(ctbl->fprt);
-
- }